﻿using System.Collections.Generic;
using System.Linq;
using Dexter.Async;
using Dexter.Async.Executor;
using Dexter.Async.Services;
using Dexter.Domain.Model;
using Dexter.Domain.Results;
using Dexter.Repository;

namespace Dexter.Search.Lucene {
	public class IndexService : AsyncService , IIndexService {
		readonly IPostRepository postRepository;
		readonly ISearchService searchService;

		public IndexService (IAsyncExecutor asyncExecutor , IPostRepository postRepository , ISearchService searchService ) : base ( asyncExecutor ) {
			this.postRepository = postRepository;
			this.searchService = searchService;
		}

		#region IIndexService Members

		public void RebuildIndex ( ) {
			IPagedResult <Post> posts = postRepository.GetPublished ( 0 , 10000000 , false , false );
			searchService.IndexItems (posts.Result);
		}

		public VoidAsyncResult RebuildIndexAsync ( ) {
			return base.AsyncCall ( RebuildIndex , null );
		}

		#endregion

		void RebuildIndex ( object state ) {
			RebuildIndex ( );
		}
	}
}